<?php

/* 购物车 cart */
class CartModel extends BaseModel
{
    var $table  = 'cart';
    var $prikey = 'rec_id';
    var $_name  = 'cart';

    var $_relation = array(
        'belongs_to_store'  => array(
            'type'      =>  BELONGS_TO,
            'model'     =>  'store',
            'reverse'   =>  'has_cart',
        ),
        'belongs_to_goodsspec'  => array(
            'type'      =>  BELONGS_TO,
            'model'     =>  'goodsspec',
            'foreign_key' => 'spec_id',
            'reverse'   =>  'has_cart_items',
        ),
    );

    /**
     *    获取商品种类数
     *
     *    @author    Garbin
     *    @return    void
     */
    function get_kinds($sess_id, $user_id = 0)
    {
        $where_user_id = $user_id ? " AND user_id={$user_id}" : '';
        $kinds = $this->db->getOne("SELECT COUNT(DISTINCT goods_id) as c FROM {$this->table} WHERE session_id='{$sess_id}'{$where_user_id}");

        return $kinds;
    }
    

    //此处用来统一处理 价格 当用户同时购买产品的时候  用来选择 哪一个价格
    function get_spec_price($spec_info,$user_id,$promotions_id=0) {
        //选择最小的价格
        $min_price = $spec_info['price'];
        
        $spec_info['goods_old_price'] = $spec_info['price'];
        $spec_info['goods_type'] = GOODS_TYPE_NORMAL;

        /* 读取促销价格 */
        $promotion_mod = &m('promotion');
        $promotion_price = $promotion_mod->get_promotion_price($spec_info['goods_id'], $spec_info['spec_id']);
        if ($promotion_price < $min_price) {
            $spec_info['promotions_id'] = $promotion_mod->getOne("SELECT pro_id FROM ".$promotion_mod->table." WHERE start_time<=" . gmtime() . " AND end_time>=" . gmtime() . " AND goods_id=" . $spec_info['goods_id']);
            $spec_info['goods_type'] = GOODS_TYPE_PRO;
            $min_price = $promotion_price;
        }

        //把原价改为会员所获得的优惠价
        if ($spec_info['if_open'] == 1) {
            $gradegoods_mod = &m('gradegoods');
            $discount = $gradegoods_mod->get_user_discount($user_id, $spec_info['goods_id']);
            if ($discount > 0) {
                $discount_price = round($spec_info['price'] * $discount, 2);
                //判断取最低价
                if ($discount_price < $min_price) {
                    $spec_info['goods_type'] = GOODS_TYPE_VIP;
                    $min_price = $discount_price;
                }
            }
        }
        $spec_info['price']  = $min_price;
        
        if ($promotions_id) {
            $ju_mod = &m('ju');
            $groupbuy = $ju_mod->get('status=1 AND goods_id=' . $spec_info['goods_id']);
            if ($groupbuy) {
                $spec_price = unserialize($groupbuy['spec_price']);
                if ($spec_price[$spec_info['spec_id']]['price'] && $spec_info['price'] > $spec_price[$spec_info['spec_id']]['price']) {
                    $min_price = $spec_price[$spec_info['spec_id']]['price'];
                    $spec_info['goods_type'] = GOODS_TYPE_GROUP;
                    $spec_info['price'] = $min_price;
                    $spec_info['max_per_user'] = $groupbuy['max_per_user'];
                    $spec_info['promotions_id'] = $groupbuy['group_id'];
                }
            }
        }
        return $spec_info;
    }
    
    
}

?>